package com.fingard.dsp.bank.directbank.yzb01;

import java.net.URLEncoder;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.xml.XmlTextWriter;
/**
* @Author Green
* */
public class YZB01Base extends DirectBase {

    /**同行1*/
    String sameBank = "1";
    /**跨行0*/
    String notSameBank = "0";
    /**对私*/
    String isPrivate = "1";

    public static final String BANK_RET_RIGHT_CODE = "0000";

    protected String getCharset(){
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String pReqStr,String opName) throws Exception{
        WriteBankLogStep2(pReqStr);
        String[] retStr = new String[]{"",""};
        String signRetStr = pReqStr;

        signRetStr = URLEncoder.encode(signRetStr,getCharset());

        String tmpAddrUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort+"/b2e/" + opName + ".do?userPassword=" + bankFront.loginPasswd + "&SIGDATA=1";

        String sendInfo = pReqStr;
        WriteBankLogLn("2.提交银行的地址：" + tmpAddrUrl);
        WebRequest tmpWebRequest = new WebRequest(tmpAddrUrl);
        tmpWebRequest.setConnectTimeout(10000);
        tmpWebRequest.setRequestProperty("User-Agent", "API");

        byte[] tmpUpBytes = sendInfo.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());

        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    protected void writeCommonHead(XmlTextWriter xmlWriter, YZB01Header header){
        xmlWriter.writeStartDocument();
        xmlWriter.directWrite("<?xml version=\"1.0\" encoding=\"GBK\" ?>");
        xmlWriter.writeStartElement("root");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("OpName",header.transCode);
        xmlWriter.writeElementString("OpChannel","01");
        xmlWriter.writeElementString("OpID",header.reqSeqNo);
        if (header.reqDateTime == null) {
            header.reqDateTime = new Date();
        }
        //交易日期
        xmlWriter.writeElementString("OpDate", Format.Date8Format.format(header.reqDateTime));
        //交易时间
        xmlWriter.writeElementString("OpTime", Format.Time6Format.format(header.reqDateTime));
        xmlWriter.writeEndElement();

    }


}
